import {
  getStatusInfoOverview,
  getCurrentInfo,
  getPredictInfo,
  getBellowLocation,
  getMainData,
  getBellowEvaluation,
  getLatestParam,
  dataGroup,
  updateRule, getRule
} from '@/services/fengxiang';

const initState = {
  statusInfoOverview: null,
  currentInfo: [],
  predictInfo: [],
  bellowLocation: {
    bellow_locations: []
  },
  latestParam:{},
  ruleResult:{
    locationFrom: "/",
    locationTo: "/",
    tempFrom: "/",
    tempTo: "/"
  }
};

const Fengxiang = {
  namespace: 'fengxiang',
  state: initState,
  effects: {
    //异步方法,应该通常用于前后端交互
    * statusInfoOverview({payload}, {call, put}) {
      const response = yield call(getStatusInfoOverview, payload);
      // dispatch转发action
      yield put({
        type: 'saveStatusInfo',
        payload: response.detail,
      });
    },
    * currentInfo({payload}, {call, put}) {
      const response = yield call(getCurrentInfo, payload);
      // dispatch转发action
      yield put({
        type: 'saveCurrentInfo',
        payload: response.detail,
      });
    },
    * predictInfo({payload}, {call, put}) {
      const response = yield call(getPredictInfo, payload);
      // dispatch转发action
      yield put({
        type: 'savePredictInfo',
        payload: response.detail,
      });
    },
    * bellowLocation({payload}, {call, put}) {
      const response = yield call(getBellowLocation, payload);
      // dispatch转发action
      yield put({
        type: 'saveBellowLocation',
        payload: response.detail,
      });
    },
    * getMainData({payload}, {call, put}) {
      const response = yield  call(getMainData, payload);
      // dispatch转发action
      yield put({
        type: 'saveMainData',
        payload: response.detail,
      })
    },
    * bellowEvaluation({payload}, {call, put}) {
      const response = yield  call(getBellowEvaluation, payload);
      // dispatch转发action
      yield put({
        type: 'saveBellowEvaluation',
        payload: response.detail,
      })
    },
    * getLatestParam({payload}, {call, put}) {
      const response = yield  call(getLatestParam,payload);
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name:'latestParam'
      })
    },
    * dataGroup({payload},{call,put}) {
      const response = yield  call(dataGroup,payload);
      yield put({
        type: "saveValues",
        payload: response.detail,
        name: 'dataGroup'
      })
    },
    * updateRule({payload},{call,put}) {
      const response = yield  call(updateRule,payload);
      yield put({
        type: "saveValues",
        payload: response.detail,
        name: 'ruleResult'
      })
    },
    * getRule({payload},{call,put}) {
      const response = yield  call(getRule,payload);
      yield put({
        type: "saveValues",
        payload: response.detail,
        name: 'ruleResult'
      })
    }
  },
  reducers: {
    // 同步方法
    saveStatusInfo(state, {payload}) {
      return {...state, statusInfoOverview: payload};
    },
    saveCurrentInfo(state, {payload}) {
      return {...state, currentInfo: payload};
    },
    savePredictInfo(state, {payload}) {
      return {...state, predictInfo: payload};
    },
    saveBellowLocation(state, {payload}) {
      return {...state, bellowLocation: payload};
    },
    saveMainData(state, {payload}) {
      return {...state, mainData: payload};
    },
    saveBellowEvaluation(state, {payload}) {
      return {...state, bellowEvaluation: payload};
    },
    saveValues(state, {payload, name}) {
      const v = {...state};
      v[name] = payload;
      return v
    },
  },
};
export default Fengxiang;
